Golang : How to setup a disk space used monitoring service with Telegram bot
Ok, writing this simple example for a friend. She wants to create a simple Telegram notification/alert for her company's system administrator to use. Basically, this is a simple program that will check a server's disk used percentage every 15 seconds.
If the disk usage percentage exceeded a threshold, start notifying the system administrator via Telegram message.
To get this program to run, you will need to supply your own Telegram Bot token
and chat ID
.
Here you go!
package main
import (
"fmt"
"log"
"strconv"
"time"
"github.com/shirou/gopsutil/disk"
tgbotapi "gopkg.in/telegram-bot-api.v4"
)
func main() {
telegramBotToken := ""
telegramChatID := ""
fmt.Println("Telegram configurations")
fmt.Println("BotToken : ", telegramBotToken)
fmt.Println("ChatID : ", telegramChatID)
bot, err := tgbotapi.NewBotAPI(telegramBotToken)
if err != nil {
log.Println(err)
} else {
log.Printf("Telegram authorized on account %s with chat ID %s\n", bot.Self.UserName, telegramChatID)
log.Println("Type /help in your Telegram app to see the available commands.", bot.Self.UserName, telegramChatID)
}
telegramChatID64, _ := strconv.ParseInt(telegramChatID, 10, 64) // use base 10 for sanity
msg := tgbotapi.NewMessage(telegramChatID64, "Disk usage monitor Bot started!")
msg.ParseMode = "markdown"
bot.Send(msg)
go diskUsageMonitor(bot, telegramChatID64)
select {} // cause this program to run forever until termination
}
func diskUsageMonitor(bot *tgbotapi.BotAPI, telegramChatID64 int64) {
diskStat, err := disk.Usage("/")
if err != nil {
log.Println(err)
}
sizeGB := 1 << (10 * 3)
counter := time.Tick(time.Duration(15) * time.Second) // poll every 15 seconds
for range counter {
total := float64(diskStat.Total) / float64(sizeGB)
used := float64(diskStat.Used) / float64(sizeGB)
free := float64(diskStat.Free) / float64(sizeGB)
log.Printf("Total disk space : %s bytes or %.2f GB\n", strconv.FormatUint(diskStat.Total, 10), total)
log.Printf("Used disk space : %s bytes or %.2f GB\n", strconv.FormatUint(diskStat.Used, 10), used)
log.Printf("Free disk space : %s bytes or %.2f GB\n", strconv.FormatUint(diskStat.Free, 10), free)
log.Println("Pecentage of disk space used : ", strconv.FormatFloat(diskStat.UsedPercent, 'f', 2, 64), "%")
if diskStat.UsedPercent > 60.0 { // harcoded here, you probably might want to make it adjustable from a config file.
data := "Pecentage of disk space used : " + strconv.FormatFloat(diskStat.UsedPercent, 'f', 2, 64) + "%"
msg := tgbotapi.NewMessage(telegramChatID64, data)
msg.ParseMode = "markdown"
bot.Send(msg)
}
}
}
References :
See also : Golang : Get hardware information such as disk, memory and CPU usage
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.2k Golang : Generate Interleaved 2 inch by 5 inch barcode
+9.7k Golang : Get current, epoch time and display by year, month and day
+8.2k Golang: Prevent over writing file with md5 hash
+14.6k Golang : Find commonalities in two slices or arrays example
+7.6k Gogland : Where to put source code files in package directory for rookie
+5.1k Python : Create Whois client or function example
+18.4k Golang : Write file with io.WriteString
+21.7k Golang : How to reverse slice or array elements order
+4.8k Python : Find out the variable type and determine the type with simple test
+4.8k Golang : Calculate a pip value and distance to target profit example
+16.7k Golang : How to generate QR codes?
+51.7k Golang : How to get time in milliseconds?